const router = require('express').Router()
const sql = require('./../sql')
const User = require('./../sql/models/User')
const Product = require('./../sql/models/Product')
const Cart = require('./../sql/models/Cart')
const utils = require('./../utils')

router.get('/getPersonalCart', (req, res, next) => {
  const { userid } = req.query
  sql.find(Cart, { userid }, { _id: 0, __v: 0}).then(data1 => {
    if (data1.length === 0) {
      res.status('200').send({
        code: '10009',
        message: '购物车空空如也'
      })
    } else {
      const arr = []
      data1.forEach(item => {
        arr.push(sql.find(Product, { proid: item.proid }))
      })
      Promise.all(arr).then(result => {
        // result [[{}], []]
        const data = []
        result.forEach((item, index) => {
          data.push({
            userid: userid,
            proid: item[0].proid,
            proname: item[0].proname,
            originprice: item[0].originprice,
            discount: item[0].discount,
            img1: item[0].img1,
            num: data1[index].num,
            flag: data1[index].flag,
            cartid:data1[index].cartid
          })
        })
    
        res.status('200').send({
          code: '200',
          message: '',
          data: data
        })
      })
    }
  })
})

router.post('/addCart', (req, res, next) => {
  let { proid, userid, num } = req.body
  num = num * 1 || 1
  sql.find(Cart, {proid, userid}, {_id:0, __v:0}).then(data1 => {
    if (data1.length === 0) {
      // 插入
      const insertData = {
        cartid: 'cart_' + utils.getUuid(),
        userid,
        proid,
        num,
        flag: true
      }
      sql.insert(Cart, insertData).then(() => {
        res.status('200').send({
          code: '200',
          message: '加入购物车成功'
        })
      })
    } else {
      // 更新
      sql.update(Cart, { userid, proid }, { $inc: { num: num },$set:{flag:true}}).then(() => {
        res.status('200').send({
          code: '200',
          message: '加入购物车成功'
        })
      })
    }
  })
})
router.get('/deleteCart',(req,res,next)=>{
  const {cartid}=req.query
  sql.delete(Cart,{cartid}).then(data=>{
    res.status('200').send({
      code: '200',
      message: '删除购物车成功'
    })
  })
})
router.get('/updateCartNum',(req,res,next)=>{
  const {userid,cartid,num  }=req.query

  sql.update(Cart,{cartid,userid},{$set:{num}}).then(()=>{
    res.status('200').send({
      code: '200',
      message: '更新购物车数量成功'
    })
  })
})
router.get('/updateCartFlag',(req,res,next)=>{
  const {cartid,flag}=req.query
  sql.update(Cart,{cartid,userid},{$set:{flag}}).then(()=>{
    res.status('200').send({
      code:'200',
      message:'更新购物车选中状态成功'
    })
  })
})
router.get('/updateCartAllFlag',(req,res,next)=>{
  const {flag,userid}=req.query
  sql.update(Cart,{userid},{$set:{flag}},1).then(()=>{
    res.status('200').send({
      code:'200',
      message:'更新购物车全部选中状态成功'
    })
  })
})
router.get('/cartRecommend', (req, res, next) => {
  sql.find(Product, { isrecommend: 1 }, { _id: 0, __v: 0 }).then(data => {
    res.status('200').send({
      code: '200',
      message: '购物车的推荐商品',
      data
    })
  })
})
module.exports = router